package com.ef.domain;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.ef.constant.EClassEntityType;
import com.ef.constant.EClassType;
import com.ef.constant.ETimeline;
import com.ef.constant.StatusType;

@Entity
public class ClassEntity implements Serializable {
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;

	@ManyToOne(fetch = FetchType.LAZY)
	private Teacher teacher;

	@ManyToOne(fetch = FetchType.LAZY)
	private Classroom classroom;

	@ManyToOne(fetch = FetchType.LAZY)
	private ClassType classType;

	@ManyToOne(fetch = FetchType.LAZY)
	private ClassLevel classLevel;

	@ManyToOne(fetch = FetchType.LAZY)
	private ClassFocus classFocus;
	@Column(length = 100)
	private String topic;
	/**
	 * 订课人数
	 */
	private Integer bookingCount = 0;
	/**
	 * 全部的人数
	 */
	private Integer allStudent = 0;

	/**
	 * 完成check的次数,为 -1 表示确认,不允许修改
	 */
	private Integer checkedTimes = 0;

	/**
	 * 本课程的属性
	 */
	@Enumerated(value = EnumType.STRING)
	private EClassEntityType type = EClassEntityType.NORMAL;

	@Temporal(TemporalType.DATE)
	private Date classDate;

	@Enumerated(value = EnumType.STRING)
	private ETimeline classTime;

	@Enumerated(value = EnumType.STRING)
	private StatusType status = StatusType.VALID;

	@Column(length = 1000)
	// cyg commons 作为Title使用
	private String commons;

	/**
	 * 由其自身判断是否已满
	 * 
	 * @return
	 */
	public Boolean isFull() {
		if (type.equals(EClassEntityType.PSA)) {
			if (this.bookingCount < 2) {
				return false;
			} else {
				return true;
			}
		}
		if (type.equals(EClassEntityType.VIP)) {
			if (this.bookingCount < 1) {
				return false;
			} else {
				return true;
			}
		}
		if (this.isF2F()) {
			if (this.bookingCount < 4) {
				return false;
			} else {
				return true;
			}
		}
		if (this.isInterview()) {
			if (this.bookingCount < 2) {
				return false;
			} else {
				return true;
			}
		}
		if (classroom != null) {
			int cap = classroom.getCapacity();
			if (bookingCount >= cap) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public boolean haveWaitingStudent() {
		if (allStudent != null && bookingCount != null) {
			return this.allStudent > this.bookingCount;
		} else {
			return false;
		}
	}

	public Integer getBookingCount() {
		return bookingCount;
	}

	public void setBookingCount(Integer bookingCount) {
		this.bookingCount = bookingCount;
	}

	public void addBookingCount(boolean booking) {
		if (booking) {
			this.bookingCount++;
		}
		this.allStudent++;
	}

	public void onlyAddBookingCount() {
		this.bookingCount++;
	}
	
	public void onlyMinusAllCount() {
		this.allStudent--;
	}

	public void minusBookingCount() {
		if (bookingCount > 0) {
			this.bookingCount = bookingCount - 1;
		}
		if (allStudent > 0) {
			allStudent = allStudent - 1;
		}
		if (allStudent < 1) {
			this.classLevel = null;
			this.type = EClassEntityType.NORMAL;
		}
	}

	public boolean needJudgeNet() {
		if (type != null) {
			if (type.equals(EClassEntityType.FREEF2F)) {
				return true;
			} else if (type.equals(EClassEntityType.NORMAL)) {
				if (this.classType != null && classType.getType() != null
						&& classType.getType().equals(EClassType.F2F)) {
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public ClassFocus getClassFocus() {
		return classFocus;
	}

	public void setClassFocus(ClassFocus classFocus) {
		this.classFocus = classFocus;
	}

	public ETimeline getClassTime() {
		return classTime;
	}

	public void setClassTime(ETimeline classTime) {
		this.classTime = classTime;
	}

	public Date getClassDate() {
		return classDate;
	}

	public void setClassDate(Date classDate) {
		this.classDate = classDate;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public Classroom getClassroom() {
		return classroom;
	}

	public void setClassroom(Classroom classroom) {
		this.classroom = classroom;
	}

	public ClassLevel getClassLevel() {
		return classLevel;
	}

	static protected SimpleDateFormat dateformat = new SimpleDateFormat(
			"MM-dd HH:mm");

	public void addBookingStudent(LeadsClassEntity ce) {
		StringBuffer tooltip = new StringBuffer();
		Leads leads = ce.getLeads();
		if (ce.getClassOrder() != null) {
			tooltip.append(leads.getBookingInfo()).append("[").append(ce.getClassOrder()).append("]").append(".\n");
		}else{
	    	tooltip.append(leads.getBookingInfo()).append(".\n");
		}
		if (this.commons == null) {
			commons = tooltip.toString();
		} else {
			commons = commons + tooltip.toString();
		}
	}
	
	public void changeBookingStudentStatus(LeadsClassEntity ce) {
		StringBuffer tooltip = new StringBuffer();
		Leads leads = ce.getLeads();
		tooltip.append(leads.getBookingInfo()).append("[CANCEL].\n");
		if (this.commons == null) {
			commons = tooltip.toString();
		} else {
			commons = commons + tooltip.toString();
		}
	}
	
	public void changeWaitStudentStatus(LeadsClassEntity ce) {
		StringBuffer tooltip = new StringBuffer();
		Leads leads = ce.getLeads();
		tooltip.append(leads.getBookingInfo()).append("[CLEAR].\n");
		if (this.commons == null) {
			commons = tooltip.toString();
		} else {
			commons = commons + tooltip.toString();
		}
	}

	public String getToolTip() {
		return this.getBookingMessage() + "\n" + this.getCommons();
	}

	 
	

	public void setClassLevel(ClassLevel classLevel) {
		this.classLevel = classLevel;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public ClassType getClassType() {
		return classType;
	}

	public void setClassType(ClassType classType) {
		this.classType = classType;
	}

	public boolean isPSA() {
		return type.equals(EClassEntityType.PSA);
	};

	public boolean isFreeF2F() {
		return type.equals(EClassEntityType.FREEF2F);
	}

	public boolean isVIP() {
		return type.equals(EClassEntityType.VIP);
	}

	public boolean isF2F() {
		if (classType != null) {
			if (classType.getType() != null) {
				return classType.getType().equals(EClassType.F2F);
			}
		}
		return false;
	}

	public boolean isInterview() {
		if (classType != null) {
			if (classType.getType() != null) {
				return classType.getType().equals(EClassType.INTERVIEW);
			}
		}
		return false;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		}
		if (obj instanceof ClassEntity) {
			if (((ClassEntity) obj).getId() == null) {
				return false;
			}
			return ((ClassEntity) obj).getId().equals(id);
		}
		return false;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = topic;
	}

	public String getMessage() {
		String focus = "";
		String room = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		if (this.getClassroom() != null) {
			room = getClassroom().getName();
		}
		if (focus.trim().length() > 0 && room.trim().length() > 0) {
			return focus + "@" + room;
		} else {
			return focus + room;
		}
	}

	public String getArrangeMessage() {
		String focus = "";
		String room = "";
		String type = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		if (this.getClassroom() != null) {
			room = getClassroom().getName();
		}
		if (getType().equals(EClassEntityType.PSA)) {
			type = "[PSA]";
		} else if (getType().equals(EClassEntityType.VIP)) {
			type = "[VIP]";
		} else if (getType().equals(EClassEntityType.FREEF2F)) {
			type = "[FREE]";
		}
		if (focus.trim().length() > 0 && room.trim().length() > 0) {
			return focus + type + "@" + room;
		} else {
			return focus + room;
		}
	}

	public String getCheckMessage() {
		String focus = "";
		String room = "";
		String type = "";
		String classType = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		if (this.getClassType() != null) {
			classType = "[" + getClassType().getName() + "]";
		}
		if (this.getClassroom() != null) {
			room = getClassroom().getName();
		}
		if (getType().equals(EClassEntityType.PSA)) {
			focus = "";
			classType="";
			type="[PSA]";
		} else if (getType().equals(EClassEntityType.VIP)) {
			focus = "";
			classType="";
			type="[VIP]";
		} else if (getType().equals(EClassEntityType.FREEF2F)) {
			type = "[FREE]";
		}
		String date = "";
		String time = "";
		SimpleDateFormat df = new SimpleDateFormat("yy-MM-dd E");
		if (this.classDate != null) {
			date = df.format(classDate);
		}
		if (this.classTime != null) {
			time = classTime.toString();
		}
		if (focus.trim().length() > 0 && room.trim().length() > 0) {
			return focus + classType + type + "@" + room + "[" + date + " "
					+ time + "]";
		} else {
			return focus + room;
		}
	};

	public String getCheckShortMessage() {
		String focus = "";
		String type = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		if (getType().equals(EClassEntityType.PSA)) {
			type = "[PSA]";
		} else if (getType().equals(EClassEntityType.VIP)) {
			type = "[VIP]";
		} else if (getType().equals(EClassEntityType.FREEF2F)) {
			type = "[FREE]";
		}
		return focus + type;
	};

	public String getArrangeColor() {
		if (classType != null) {
			if (classType.getType().equals(EClassType.F2F)) {
				return "#FF4040";// red
			}
			if (classType.getType().equals(EClassType.WK)) {
				return "#1E90FF";
			}
			if (classType.getType().equals(EClassType.LC)) {
				return "#7FFF00";
			}
		}
		return "#FF00FF";
	}

	public String getBookingMessage() {
		String focus = "";
		String room = "";
		String type = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		if (this.getClassroom() != null) {
			room = getClassroom().getName();
		}
		if (getType().equals(EClassEntityType.PSA)) {
			focus = "[PSA]";
		} else if (getType().equals(EClassEntityType.VIP)) {
			focus = "[VIP]";
		} else if (getType().equals(EClassEntityType.FREEF2F)) {
			type = "[FREE]";
		}
		if (focus.trim().length() > 0 && room.trim().length() > 0) {
			return allStudent + " " + focus + type + "@" + room;
		} else {
			return focus + room;
		}
	}

	public EClassEntityType getType() {
		return type;
	}

	public void setType(EClassEntityType type) {
		this.type = type;
	}

	public Integer getCheckedTimes() {
		return checkedTimes;
	}

	public void setCheckedTimes(Integer checkedTimes) {
		this.checkedTimes = checkedTimes;
	}

	public void addCheckedTimes() {
		this.checkedTimes++;
	}

	public String getResponseString() {
		StringBuffer rsp = new StringBuffer();
		// SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String focus = "";
		if (this.getClassFocus() != null) {
			focus = getClassFocus().getName();
		}
		String topic = this.getTopic();
		rsp.append("<class>").append("{id:").append(id).append(",focus:\"")
				.append(focus).append("\",topic:\"<![CDATA[").append(topic)
				.append("]]>\"}</class>");
		return rsp.toString();
	}

	public StatusType getStatus() {
		return status;
	}

	public void setStatus(StatusType status) {
		this.status = status;
	}

	public Integer getAllStudent() {
		return allStudent;
	}

	public void setAllStudent(Integer allStudent) {
		this.allStudent = allStudent;
	}

	public String getCommons() {
		if (commons == null) {
			return "";
		}
		return commons;
	}

	public void setCommons(String commons) {
		this.commons = commons;
	}
}
